<template>
  <div class="qtCompanies">
    <qtHeader fatherTitle="公司" fatherCreate="创建新的" @tell='getName'></qtHeader>
    <el-row>
      <el-col :span="16">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="companies" label="公司名" width="400">
          </el-table-column>
          <el-table-column prop="employees" label="员工" width="120">
            <template slot-scope="scope">{{scope.row.total}}名</template>
          </el-table-column>
          <el-table-column prop="address" label="地址" width="400" ></el-table-column>
          <el-table-column label="操作" >
        <template slot-scope="scope" >
          <el-button size="mini" type="warning" @click="handleEdit(scope.row)">
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
            <i class="el-icon-delete"></i>
          </el-button>
        </template>
      </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="8"></el-col>
    </el-row>
    <!-- 添加弹框 -->
    <el-dialog  :visible.sync="dialogFormVisible" width="800px"  center custom-class="dialogClass" @close="cancel">
      <el-form :model="form" ref="dataform">
        <el-form-item label="公司名" :label-width="formLabelWidth" prop="companies">
          <el-input v-model="form.companies" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qtHeader from '@/components/header.vue'
import { getCompanies, addCompanies, delCompanies, checkCompanies } from '@/api/index.js'
export default {
  components: {
    qtHeader
  },
  data () {
    return {
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '200px',
      row: '',
      assetsId: '',
      form: {
        companies: '',
        address: ''
      }
    }
  },
  created () {
    this.getcompanies()
  },
  methods: {
    getcompanies () {
      getCompanies()
        .then(res => {
          // console.log(res.data.data)
          this.tableData = res.data.data
        })
    },
    getName (name) {
      this.dialogFormVisible = name
    },
    cancel () {
      this.assetsId = ''
      this.$refs.dataform.resetFields()
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    confirm () {
      var obj = {
        companies: this.form.companies,
        address: this.form.address
      }
      if (this.assetsId) {
        obj.id = this.assetsId
        if (this.row.companies !== this.form.companies) {
          obj.companies = this.form.companies
        } else {
          obj.companies = this.row.companies
        }
        if (this.row.address !== this.form.address) {
          obj.address = this.form.address
        } else {
          obj.address = this.row.address
        }
        // 修改公司
        checkCompanies(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
            this.getcompanies()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      } else {
        // 新建公司
        addCompanies(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('新建公司成功')
            this.getcompanies()
          } else {
            this.$message.error('新建公司失败')
          }
        })
      }

      this.dialogFormVisible = false
    },
    handleEdit (row) {
      this.getName(name)
      this.row = row
      console.log(row)
      this.assetsId = row.id
      this.form.companies = row.companies
      this.form.address = row.address
    },
    handleDelete (row) {
      this.$confirm('是否删除资产?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delCompanies(row.id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
          // 判断当删除的数据为当前页中的最后一条数据，刷新页面的时候需要把当前页码数减1
          if (this.total / this.pageSize - parseInt(this.total / this.pageSize) <= 0.1) {
            this.page = this.page - 1
          }
          this.getcompanies()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.qtCompanies {
  padding: 10px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.qtTitle {
  font-size: 25px;
  text-align: left;
  line-height: 1;
}
.qtButton {
  margin-top: 5px;
  text-align: right;
  padding-right: 20px;
  .qtCreateAsset {
    background-color: #3c8dbc;
    padding: 5px 10px;
    color: #fff;
    border-radius: 5px;
  }
}

</style>
